package com.canbot.u05.adapter.sales;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.canbot.u05.MainApplication;
import com.canbot.u05.R;
import com.canbot.u05.bean.sales.ChoosePicBean;
import com.canbot.u05.glide.GlideApp;
import com.canbot.u05.socket.IpMsgConst;

import java.util.ArrayList;
import java.util.List;

public class ItemSelectMapSalesAdapter2 extends BaseAdapter {

        private List<ChoosePicBean> objects = new ArrayList<ChoosePicBean>();

        private Context context;

        private LayoutInflater inflater;

        private ViewHolder holder;

        private int currentPos = -1;//current

        public ItemSelectMapSalesAdapter2(Context context, List<ChoosePicBean> objects) {
                this.context = context;
                this.inflater = LayoutInflater.from(context);
                this.objects = objects;
        }

        @Override
        public int getCount() {
                return objects.size();
        }

        @Override
        public ChoosePicBean getItem(int position) {
                return objects.get(position);
        }

        @Override
        public long getItemId(int position) {
                return position;
        }

        @Override
        public View getView(final int position, View convertView, final ViewGroup parent) {
                if (convertView == null) {
                        convertView = inflater.inflate(R.layout.item_select_map_sales, null);
                        holder = new ViewHolder(convertView);
                        convertView.setTag(holder);
                }
                final ViewHolder holder = (ViewHolder) convertView.getTag();
                ChoosePicBean map = objects.get(position);
                String imagePath = map.getImagePath();
                Log.d("ItemSelectMapSales", imagePath + "");
//        MainApplication.instance.displayImage(ApiUtil.HTTP+imagePath,holder.ivMap);
                GlideApp.with(parent.getContext()).clear(holder.ivMap);
                GlideApp.with(parent.getContext())
                        .asDrawable()
                        .load(IpMsgConst.HTTP_URL_CHEST + imagePath.replace("/uupic/","/uupic_thumb/"))
                        .thumbnail(0.1f)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
                        .skipMemoryCache(false)
                        .placeholder(R.drawable.sales_banner01)//占位图
                        .error(R.drawable.ic_picture_loadfailed)
                        .override(100, 100)
                        .into(holder.ivMap);
                if(imagePath.contains("/uupic/")){
                        holder.textView.setText(imagePath.replace("/uupic/",""));
                }
                holder.cbSelect.setTag("tag" + position);
                boolean checked = objects.get(position).isChecked();
                holder.cbSelect.setChecked(checked);
                if (checked) {
                        currentPos = position;
                }

                holder.cbSelect.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                                if (currentPos >= 0) {
                                        if (currentPos == position) {
                                                ChoosePicBean map = objects.get(position);
                                                map.setChecked(false);
                                                objects.set(position, map);
                                                holder.cbSelect.setChecked(false);
                                                currentPos = -1;

                                        }
                                        else {
                                                ChoosePicBean map1 = objects.get(currentPos);
                                                map1.setChecked(false);
                                                objects.set(currentPos, map1);
                                                CheckBox cbOldSelect = (CheckBox) parent.findViewWithTag("tag" + currentPos);
                                                if (cbOldSelect != null) {
                                                        cbOldSelect.setChecked(false);
                                                }

                                                ChoosePicBean map2 = objects.get(position);
                                                map2.setChecked(true);
                                                objects.set(position, map2);
                                                holder.cbSelect.setChecked(true);
                                                currentPos = position;
                                        }

                                }
                                else {
                                        ChoosePicBean map = objects.get(position);
                                        map.setChecked(true);
                                        objects.set(position, map);
                                        holder.cbSelect.setChecked(true);

                                        currentPos = position;
                                }
                        }
                });


                return convertView;
        }


        protected class ViewHolder {

                private ImageView ivMap;

                private CheckBox cbSelect;

                private RelativeLayout rl_layout;

                private TextView textView;

                public ViewHolder(View view) {
                        ivMap = (ImageView) view.findViewById(R.id.iv_map);
                        cbSelect = (CheckBox) view.findViewById(R.id.cb_select);
                        rl_layout = (RelativeLayout) view.findViewById(R.id.rl_layout);
                        textView = (TextView) view.findViewById(R.id.tv);
                }
        }


}
